﻿//Copyright (c) Chris Pietschmann 2013 (http://pietschsoft.com)
//Licensed under the GNU Library General Public License (LGPL)
//License can be found here: http://easypatterns.codeplex.com/license

using System;

namespace EasyPatterns.Factory
{
    /// <summary>
    /// A LambdaFactory for creating instances of the specified Type.
    /// </summary>
    /// <typeparam name="TReturnType">The Type the factory will create.</typeparam>
    public class LambdaFactory<TReturnType> : Factory<TReturnType, TReturnType>
    {
        /// <summary>
        /// LambdaFactory Constructor
        /// </summary>
        /// <param name="createInstanceExpression">The expression used within the Factory for creating new instances of the specified Type.</param>
        public LambdaFactory(Func<TReturnType> createInstanceExpression)
        {
            if (createInstanceExpression == null)
            {
                throw new ArgumentNullException("createInstanceExpression");
            }
            this.CreateInstanceExpression = createInstanceExpression;
        }

        /// <summary>
        /// The internal expression used within this Factory for creating new instances of the specified Type.
        /// </summary>
        protected Func<TReturnType> CreateInstanceExpression { get; set; }

        /// <summary>
        /// Creates a new instance of the specified Type.
        /// </summary>
        /// <returns>An instance of the specified Type.</returns>
        protected override TReturnType CreateInstance()
        {
            return this.CreateInstanceExpression();
        }
    }
}
